package com.purong.purong.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.purong.purong.Contant;
import com.purong.purong.PermissionConstant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.activity.CreditDetialActivity;
import com.purong.purong.activity.CreditSearchActivity;
import com.purong.purong.activity.ArticleDetActivity;
import com.purong.purong.activity.InfoCenterActivity;
import com.purong.purong.activity.IntentionActivity;
import com.purong.purong.activity.LoanSearchActivity;
import com.purong.purong.activity.LocationActivity;
import com.purong.purong.activity.BankLoanDetialActivity;
import com.purong.purong.activity.LoginActivity;
import com.purong.purong.activity.MainActivity;
import com.purong.purong.activity.PersionCheckingActivity;
import com.purong.purong.activity.SmallLoanDetialActivity;
import com.purong.purong.activity.WebActivity;
import com.purong.purong.adapter.HomeArticleAdapter;
import com.purong.purong.adapter.HomeCreditcardAdapter;
import com.purong.purong.adapter.HomeBankLoanAdapter;
import com.purong.purong.bean.ADInfo;
import com.purong.purong.bean.AdListBean;
import com.purong.purong.bean.ArticleBean;
import com.purong.purong.bean.ArticleRecommBean;
import com.purong.purong.bean.CreditCardBean;
import com.purong.purong.bean.CreditRecommBean;
import com.purong.purong.bean.BankLoanBean;
import com.purong.purong.bean.BankLoanRecommBean;
import com.purong.purong.bean.AreaBean;
import com.purong.purong.bean.SmallLoanBean;
import com.purong.purong.bean.SmallLoanRecommBean;
import com.purong.purong.util.AmapLocation;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.purong.purong.widget.FullyLinearLayoutManager;
import com.purong.purong.widget.GradationScrollView;
import com.purong.purong.widget.LocationDialog;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.glide.GlideUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;
import me.grantland.widget.AutofitTextView;


/**
 * Created by user on 2017/9/12.
 */
public class HomeFragment extends BasePermFragment implements GradationScrollView.ScrollViewListener {
    private GradationScrollView mScrollView;
    private RadiusImageView smallLoanImg;
    private AutofitTextView smallLoanMoney;
    private TextView titleName,locationCity,smallLoanName,smallLoanLending,smallLoanRate,smallLoanPeriod;
    private ImageView statusBar,topCbBg,centerCbBg,loanIntentImg,creditAllImg;
    private RelativeLayout titleBar;
    private LinearLayout topIndicaLay,centerIndicaLay,locationLay,creditTitle,loanTitle,articleTitle,smallLoanLay;
    private ImageView[] topIndicators,centerIndicators;
    private ConvenientBanner topCb, centerCb;
    private RecyclerView creditRecycler,loanRecycler,articleRecycler;
    private HomeBankLoanAdapter mLoanAdapter;
    private HomeCreditcardAdapter mCreditAdapter;
    private HomeArticleAdapter mArticleAdapter;
    private List<CreditCardBean> creditList=new ArrayList<>();
    private List<BankLoanBean> loanList=new ArrayList<>();
    private List<ArticleBean> articleList=new ArrayList<>();
    private AmapLocation mLocation;
    private boolean hasStartLocation,hasCheckPer;
    private AreaBean mLocationAreaBean,mChoseArea;
    private int titleBarRange;
    private boolean getAdSuc,getCreditSuc, getBankLoanSuc,getSmallLoanSuc,getArticleSuc;
    private PtrPuRongLayout mPtrFrame;
    private SmallLoanBean smallLoanBean;
    @Override
    public void initView(View v) {
        titleBarRange=CommonUtil.dip2px(getActivity(),100);
        mPtrFrame = (PtrPuRongLayout) v.findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {

                return false;

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getAd();
                getRecommCredit();
                getRecommBankLoan();
                getRecommSmallLoan();
                getRecommArticle();
                if (!getAdSuc&&!getCreditSuc&&!getBankLoanSuc &&!getArticleSuc){
                    if (!hasDestroy())
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                if (mPtrFrame.isRefreshing()) {
                                    mPtrFrame.refreshComplete();
                                }
                            }
                        }, 100);
                }
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {

                return PtrDefaultCustomHandler.checkContentCanBePulledDown(frame, content, header);

            }
        });
        smallLoanImg=(RadiusImageView)v.findViewById(R.id.small_loan_img);
        smallLoanName=(TextView)v.findViewById(R.id.small_loan_name);
        smallLoanMoney=(AutofitTextView)v.findViewById(R.id.small_loan_money);
        smallLoanRate=(TextView)v.findViewById(R.id.small_loan_rate);
        smallLoanLending=(TextView)v.findViewById(R.id.small_loan_lending);
        smallLoanPeriod=(TextView)v.findViewById(R.id.small_loan_period);
        loanIntentImg=(ImageView)v.findViewById(R.id.loan_intent_img);
        creditAllImg=(ImageView)v.findViewById(R.id.credit_all_img);
        int imgWidth=(CommonUtil.getScreenWidth(getActivity())-CommonUtil.dip2px(getActivity(),48))/2;
        int imgHeight=(int)(imgWidth/3.346);
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(imgWidth,imgHeight);
        loanIntentImg.setLayoutParams(lp);
        creditAllImg.setLayoutParams(lp);
        loanIntentImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), IntentionActivity.class);
                startActivity(intent);
            }
        });
        creditAllImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), CreditSearchActivity.class);
                startActivity(intent);
            }
        });
        locationCity=(TextView)v.findViewById(R.id.location_city);
        creditRecycler =(RecyclerView) v.findViewById(R.id.creditcard_recycler_view);
        loanRecycler=(RecyclerView) v.findViewById(R.id.loan_recycler_view);
        articleRecycler=(RecyclerView)v.findViewById(R.id.article_recycler_view);
        topIndicaLay = (LinearLayout) v.findViewById(R.id.top_indicator_lay);
        centerIndicaLay = (LinearLayout) v.findViewById(R.id.center_indicator_lay);
        smallLoanLay=(LinearLayout)v.findViewById(R.id.small_loan_lay);
        smallLoanLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (smallLoanBean!=null){
                    Intent intent=new Intent(getActivity(),SmallLoanDetialActivity.class);
                    intent.putExtra("LOAN",smallLoanBean);
                    startActivity(intent);
                }
            }
        });
        creditTitle=(LinearLayout)v.findViewById(R.id.creditcard_title);
        loanTitle=(LinearLayout)v.findViewById(R.id.loan_title);
        articleTitle=(LinearLayout)v.findViewById(R.id.article_title);
        statusBar = (ImageView) v.findViewById(R.id.status_bar);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
            statusBar.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
        }else {
            statusBar.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CommonUtil.getStatusBarHeight(getActivity())));
        }
        titleName = (TextView) v.findViewById(R.id.title_name);
        titleBar = (RelativeLayout) v.findViewById(R.id.title_bar);
        topCbBg=(ImageView)v.findViewById(R.id.top_cb_bg);
        centerCbBg=(ImageView)v.findViewById(R.id.center_cb_bg);
        mScrollView = (GradationScrollView) v.findViewById(R.id.scrollview);
        mScrollView.setScrollViewListener(this);
        topCb = (ConvenientBanner) v.findViewById(R.id.top_cb);
        int topHeight = (int) (CommonUtil.getScreenWidth(getActivity()) * 0.56);
        topCb.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, topHeight));
        topCbBg.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, topHeight));
        centerCb = (ConvenientBanner) v.findViewById(R.id.center_cb);
        int centerHeight = (int) (CommonUtil.getScreenWidth(getActivity()) * 0.24);
        RelativeLayout.LayoutParams centerLp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, centerHeight);
        centerLp.addRule(RelativeLayout.BELOW,R.id.creditcard_recycler_lay);
        centerCb.setLayoutParams(centerLp);
        centerCbBg.setLayoutParams(centerLp);
        locationLay=(LinearLayout)v.findViewById(R.id.location_lay);
        locationLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), LocationActivity.class);
                intent.putExtra("LOCATION_AREA", mLocationAreaBean);
                startActivityForResult(intent,1);

            }
        });
        v.findViewById(R.id.info_center).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(getActivity(),InfoCenterActivity.class);
                intent.putExtra("PAGE",InfoCenterActivity.PAGE_NOTICE);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.loan_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity)getActivity()).jumpLoan();
            }
        });
        v.findViewById(R.id.creditcard_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), CreditSearchActivity.class);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.checking_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                if (PuRongApp.getLoginInfo(getActivity())!=null){
                    intent=new Intent(getActivity(), PersionCheckingActivity.class);
                    startActivity(intent);
                }else {
                    intent=new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });

        v.findViewById(R.id.more_loan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), LoanSearchActivity.class);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.more_credit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), CreditSearchActivity.class);
                startActivity(intent);
            }
        });
        v.findViewById(R.id.more_article).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity)getActivity()).jumpArticle();
            }
        });
        mCreditAdapter = new HomeCreditcardAdapter(getActivity());
        mCreditAdapter.setOnCreditClickListener(new HomeCreditcardAdapter.OnCreditClickListener() {
            @Override
            public void onCreditClick(CreditCardBean card) {
                Intent intent=new Intent(getActivity(), CreditDetialActivity.class);
                intent.putExtra("CREDIT",card);
                startActivity(intent);
            }
        });

        mCreditAdapter.setData(creditList);
        creditRecycler.setLayoutManager(new FullyLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        creditRecycler.setNestedScrollingEnabled(false);
        creditRecycler.setAdapter(mCreditAdapter);

        mLoanAdapter = new HomeBankLoanAdapter(getActivity());
        mLoanAdapter.setOnLoanClickListener(new HomeBankLoanAdapter.OnLoanClickListener() {
            @Override
            public void onLoanClick(BankLoanBean loan) {
                if (loan==null)
                    return;
                Intent intent=new Intent(getActivity(),BankLoanDetialActivity.class);
                intent.putExtra("LOAN",loan);
                startActivity(intent);
            }
        });
        mLoanAdapter.setData(loanList);
        loanRecycler.setLayoutManager(new FullyLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
//        loanRecycler.setNestedScrollingEnabled(false);
        loanRecycler.setAdapter(mLoanAdapter);

         mArticleAdapter= new HomeArticleAdapter(getActivity());
        mArticleAdapter.setOnInfoItemClickListener(new HomeArticleAdapter.OnArticleItemClickListener() {
            @Override
            public void onArticleClick(ArticleBean article) {
                Intent intent=new Intent(getActivity(), ArticleDetActivity.class);
                intent.putExtra("ARTICLE_ID",article.getArticle_id());
                startActivity(intent);
            }
        });
        mArticleAdapter.setData(articleList);
        articleRecycler.setLayoutManager(new FullyLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
//        articleRecycler.setNestedScrollingEnabled(false);
        articleRecycler.setAdapter(mArticleAdapter);
    }

    @Override
    public void initData() {
        initDefArea();
        initLocation();
//        getRecommCredit();
//        getRecommBankLoan();
//        getRecommArticle();
    }

    @Override
    public int getRootViewId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            if (resultCode==getActivity().RESULT_OK){
                AreaBean area=(AreaBean)data.getSerializableExtra("LOCATION_AREA");
                if (area!=null){
                    mChoseArea=area;
                    locationCity.setText(mChoseArea.getAcronym());
                    SharePreUtil.getInstance(getActivity()).setLoactionAreaId(mChoseArea.getArea_id());
                    SharePreUtil.getInstance(getActivity()).setLoactionParentId(mChoseArea.getParent_id());
                    SharePreUtil.getInstance(getActivity()).setLoactionAreaName(mChoseArea.getAcronym());
                    getAd();
                    getRecommCredit();
                    getRecommBankLoan();
                    getRecommSmallLoan();
                    getRecommArticle();
                }
            }
        }
    }


    private void initDefArea(){
        mChoseArea=new AreaBean();
        mChoseArea.setArea_id(SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        mChoseArea.setParent_id(SharePreUtil.getInstance(getActivity()).getLoactionParentId());
        mChoseArea.setAcronym(SharePreUtil.getInstance(getActivity()).getLoactionAreaName());
        locationCity.setText(mChoseArea.getAcronym());
    }

    private void initLocation(){
        mLocation=new AmapLocation(getActivity().getApplication());
        mLocation.setOnLocationListener(new AmapLocation.OnLocationListener() {
            @Override
            public void onLocationSuccess(AMapLocation aMapLocation) {
                Log.e("zwj","....onLocationSuccess");
                getCity(aMapLocation.getCity());
            }

            @Override
            public void onLocationFail(int code) {

            }

            @Override
            public void onReachMaxCount() {

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (mPtrFrame!=null&&mPtrFrame.isRefreshing()) {
                    mPtrFrame.refreshComplete();
                }
            }
        }, 100);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!getAdSuc)
            getAd();
//        if (!getNoticeSuc)
//            getNotice();
        if (!getCreditSuc)
            getRecommCredit();
        if (!getBankLoanSuc)
            getRecommBankLoan();
        if (!getSmallLoanSuc)
            getRecommSmallLoan();
        if (!getArticleSuc)
            getRecommArticle();

        if (hasStartLocation)
            return;
        if (Build.VERSION.SDK_INT >= 23) {
            checkPermission(!hasCheckPer);
            hasCheckPer=true;
        }else {
            if (mLocation!=null){
                mLocation.startLocation();
                hasStartLocation=true;
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocation!=null){
            mLocation.stopLocation();
            mLocation.destroy();
        }

    }

    @Override
    protected String[] getPermissions() {

        return PermissionConstant.PERMS_LOCATION;
    }

    @Override
    protected void doPermissinosSuc() {
        if (mLocation!=null)
        mLocation.startLocation();
        hasStartLocation=true;

    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.CITY)){
            if (responseInfo.isRequestOk){
                try{
                    mLocationAreaBean= JSON.parseObject(responseInfo.content,AreaBean.class);
                    if (mLocationAreaBean!=null){
                        if (mLocation!=null){
                            mLocation.stopLocation();
                        }
                        if (mChoseArea!=null&&!mChoseArea.getArea_id().equals(mLocationAreaBean.getArea_id())){
                            LocationDialog dialog=new LocationDialog(getActivity(), "定位到您的城市是" + mLocationAreaBean.getAcronym() + ",是否切换到" + mLocationAreaBean.getAcronym() + "?", new LocationDialog.OnConfirmClickListenr() {
                                @Override
                                public void onConfirm() {
                                    locationCity.setText(mLocationAreaBean.getAcronym());
                                    SharePreUtil.getInstance(getActivity()).setLoactionAreaId(mLocationAreaBean.getArea_id());
                                    SharePreUtil.getInstance(getActivity()).setLoactionParentId(mLocationAreaBean.getParent_id());
                                    SharePreUtil.getInstance(getActivity()).setLoactionAreaName(mLocationAreaBean.getAcronym());
                                }

                                @Override
                                public void onCancel() {

                                }
                            });
                            dialog.show();
                        }else {
                            locationCity.setText(mLocationAreaBean.getAcronym());
                            SharePreUtil.getInstance(getActivity()).setLoactionAreaId(mLocationAreaBean.getArea_id());
                            SharePreUtil.getInstance(getActivity()).setLoactionParentId(mLocationAreaBean.getParent_id());
                            SharePreUtil.getInstance(getActivity()).setLoactionAreaName(mLocationAreaBean.getAcronym());

                        }

                        sendBroadcast(Contant.LOCATION_ACTION, mLocationAreaBean);
                    }
                }catch (Exception e){

                }
            }
        }else if (responseInfo.url.equals(Contant.CREDIT_RECOMMEND)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try {
                    CreditRecommBean bean=JSON.parseObject(responseInfo.content,CreditRecommBean.class);
                    if (bean!=null&&bean.getCredit_recommend_list()!=null){
                        creditList.clear();
                        creditList.addAll(bean.getCredit_recommend_list());
                        mCreditAdapter.notifyDataSetChanged();
                        if (creditList.size()!=0){
                            creditTitle.setVisibility(View.VISIBLE);
                        }else {
                            creditTitle.setVisibility(View.GONE);
                        }
                        getCreditSuc=true;
                    }else {
                        getCreditSuc=false;
                    }

                }catch (Exception e){
                    getCreditSuc=false;
                }
            }else {
                getCreditSuc=false;
            }
        }else if (responseInfo.url.equals(Contant.LOAN_RECOMMEND)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try {
                    BankLoanRecommBean bean=JSON.parseObject(responseInfo.content,BankLoanRecommBean.class);
                    if (bean!=null&&bean.getLoan_recommend_list()!=null){
                        loanList.clear();
                        loanList.addAll(bean.getLoan_recommend_list());
                        mLoanAdapter.notifyDataSetChanged();
                        if (loanList.size()!=0){
                            loanTitle.setVisibility(View.VISIBLE);
                        }else {
                            loanTitle.setVisibility(View.GONE);
                        }
                        getBankLoanSuc =true;
                    }else {
                        getBankLoanSuc =false;
                    }

                }catch (Exception e){
                    getBankLoanSuc =false;
                }
            }else {
                getBankLoanSuc =false;
            }
        }else if (responseInfo.url.equals(Contant.SMALL_RECOMMEND)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try {
                    SmallLoanRecommBean bean=JSON.parseObject(responseInfo.content,SmallLoanRecommBean.class);
                    if (bean!=null&&bean.getSmall_recommend_list()!=null){
                        if (bean.getSmall_recommend_list().size()>=1){
                            smallLoanBean=bean.getSmall_recommend_list().get(0);
                            setSmallLoan(smallLoanBean);
                            smallLoanLay.setVisibility(View.VISIBLE);
                        }else {
                            smallLoanLay.setVisibility(View.GONE);
                        }
                        getSmallLoanSuc =true;
                    }else {
                        getSmallLoanSuc =false;
                    }

                }catch (Exception e){
                    getSmallLoanSuc =false;
                }
            }else {
                getSmallLoanSuc =false;
            }
        }else if (responseInfo.url.equals(Contant.ARTICLE_RECOMMEND)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    ArticleRecommBean bean=JSON.parseObject(responseInfo.content,ArticleRecommBean.class);
                    if (bean!=null&&bean.getArticle_recommend_list()!=null){
                        articleList.clear();
                        articleList.addAll(bean.getArticle_recommend_list());
                        mArticleAdapter.notifyDataSetChanged();
                        if (articleList.size()!=0){
                            articleTitle.setVisibility(View.VISIBLE);
                        }else {
                            articleTitle.setVisibility(View.GONE);
                        }
                        getArticleSuc=true;
                    }else {
                        getArticleSuc=false;
                    }

                }catch (Exception e){
                    getArticleSuc=false;
                }
            }else {
                getArticleSuc=false;
            }
        }else if (responseInfo.url.equals(Contant.AD_IMG)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    AdListBean bean=JSON.parseObject(responseInfo.content,AdListBean.class);
                    if (bean!=null&&bean.getAd_list()!=null&&bean.getAd_list().size()>0){
                        if (responseInfo.params.get("location_code").equals("HomeTop")){
                            setTopAdInfo(bean);
                        }else if (responseInfo.params.get("location_code").equals("HomeMiddle")){
                            setCenterAdInfo(bean);
                        }
                        getAdSuc=true;
                    }else {
                        getAdSuc=false;
                    }

                }catch (Exception e){
                    getAdSuc=false;
                }
            }else {
                getAdSuc=false;
            }
        }
    }

    private void getCity(String name){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if(name==null){
            params.put("city_name", "");
        }else {
            params.put("city_name", name);
        }

        requestNoToast(Contant.CITY, params, true, false);
    }
//    private void getNotice(){
//        Map<String, String> params = new LinkedHashMap<String, String>();
//        getNoticeSuc=requestNoToast(Contant.ANNOUNCEMENT_HOME, params, true, false);
//    }
    private void getRecommCredit(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("recommend_location","1");
        params.put("city_id", SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        params.put("province_id", SharePreUtil.getInstance(getActivity()).getLoactionParentId());
        getCreditSuc=requestNoToast(Contant.CREDIT_RECOMMEND, params, true, false);

    }
    private void getRecommBankLoan(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("recommend_location","1");
        params.put("city_id", SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        params.put("province_id", SharePreUtil.getInstance(getActivity()).getLoactionParentId());
        getBankLoanSuc =requestNoToast(Contant.LOAN_RECOMMEND, params, true, false);

    }
    private void getRecommSmallLoan(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("recommend_location","1");
        params.put("city_id", SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        params.put("province_id", SharePreUtil.getInstance(getActivity()).getLoactionParentId());
        getSmallLoanSuc =requestNoToast(Contant.SMALL_RECOMMEND, params, true, false);

    }
    private void getRecommArticle(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("recommend_location","1");
        getArticleSuc=requestNoToast(Contant.ARTICLE_RECOMMEND, params, true, false);

    }

    private void getAd(){
        Map<String, String> topParams = new LinkedHashMap<String, String>();
        topParams.put("location_code","HomeTop");
        topParams.put("city_id",SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        getAdSuc=requestNoToast(Contant.AD_IMG, topParams, true, false);
        Map<String, String> middleParams = new LinkedHashMap<String, String>();
        middleParams.put("location_code","HomeMiddle");
        middleParams.put("city_id",SharePreUtil.getInstance(getActivity()).getLoactionAreaId());
        getAdSuc=requestNoToast(Contant.AD_IMG, middleParams, true, false);

    }
    private void setSmallLoan(SmallLoanBean smallLoan){
        if (smallLoan==null)
            return;
        GlideUtil.getInstance().load(getActivity(),smallLoan.getSmall_product_url(),smallLoanImg,true);
        smallLoanName.setText(smallLoan.getSmall_product_name());
        smallLoanMoney.setText(CommonUtil.numConvertSubZero(Integer.valueOf(smallLoan.getMin_small_money()),2)+"~"+CommonUtil.numConvertSubZero(Integer.valueOf(smallLoan.getMax_small_money()),2));
        smallLoanRate.setText("月利率"+ smallLoan.getRate());
        smallLoanLending.setText(smallLoan.getLending_day()+"放款");
        smallLoanPeriod.setText("贷款期限"+smallLoan.getPeriod());
    }
    private void setTopAdInfo( final AdListBean adBean) {
        topCbBg.setVisibility(View.GONE);
        initTopIndicator(adBean);
        topCb.setPages(new CBViewHolderCreator<ImageViewHolder>() {
            @Override
            public ImageViewHolder createHolder() {
                return new ImageViewHolder(GlideUtil.NORMAL);
            }
        }, adBean.getAd_list());
        if (adBean.getAd_list().size()>1){
            initTopIndicator(adBean);
            topCb.startTurning(3000);
            topCb.setCanLoop(true);
        }else {
            topCb.setCanLoop(false);
        }
        topCb.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTopIndicator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        topCb.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (adBean.getAd_list().get(position).getAd_goto_url()!=null&&
                        (adBean.getAd_list().get(position).getAd_goto_url().startsWith("http")||adBean.getAd_list().get(position).getAd_goto_url().startsWith("HTTP"))){
                    Intent intent=new Intent(getActivity(),WebActivity.class);
                    intent.putExtra("TITLE", adBean.getAd_list().get(position));
                    intent.putExtra("URL", adBean.getAd_list().get(position).getAd_goto_url());
                    intent.putExtra("TITLEBAR", true);
                    startActivity(intent);
                }
            }
        });
    }
    private void setCenterAdInfo( final AdListBean adBean) {
        centerCbBg.setVisibility(View.GONE);
        centerCb.setPages(new CBViewHolderCreator<ImageViewHolder>() {
            @Override
            public ImageViewHolder createHolder() {
                return new ImageViewHolder(GlideUtil.HROIZONTAL);
            }
        }, adBean.getAd_list());
        if (adBean.getAd_list().size()>1){
            initCenterIndicator(adBean);
            centerCb.startTurning(5000);
            centerCb.setCanLoop(true);
        }else {
            centerCb.setCanLoop(false);
        }

        centerCb.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCenterIndicator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        centerCb.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (adBean.getAd_list().get(position).getAd_goto_url()!=null&&
                        (adBean.getAd_list().get(position).getAd_goto_url().startsWith("http")||adBean.getAd_list().get(position).getAd_goto_url().startsWith("HTTP"))){
                    Intent intent=new Intent(getActivity(),WebActivity.class);
                    intent.putExtra("TITLE", "");
                    intent.putExtra("URL", adBean.getAd_list().get(position).getAd_goto_url());
                    intent.putExtra("TITLEBAR", true);
                    startActivity(intent);
                }
            }
        });
    }

    private void initTopIndicator(AdListBean mAdBean) {
        topIndicaLay.removeAllViews();
        if (mAdBean != null && mAdBean.getAd_list() != null&&mAdBean.getAd_list().size()>1) {
            topIndicators = new ImageView[mAdBean.getAd_list().size()];
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(CommonUtil.dip2px(getActivity(), 10), CommonUtil.dip2px(getActivity(), 3));
            lp.setMargins(CommonUtil.dip2px(getActivity(), 3), 0, CommonUtil.dip2px(getActivity(), 3), 0);
            for (int i = 0; i < mAdBean.getAd_list().size(); i++) {
                ImageView indicatorImg = new ImageView(getActivity());
                if (i == 0) {
                    indicatorImg.setImageResource(R.drawable.lunbodian_sel);
                } else {
                    indicatorImg.setImageResource(R.drawable.lunbodian_nor);

                }
                indicatorImg.setLayoutParams(lp);
                topIndicaLay.addView(indicatorImg);
                topIndicators[i] = indicatorImg;
            }

        }
    }
    private void initCenterIndicator(AdListBean mAdBean) {
        centerIndicaLay.removeAllViews();
        if (mAdBean != null && mAdBean.getAd_list() != null&&mAdBean.getAd_list().size()>1) {
            centerIndicators = new ImageView[mAdBean.getAd_list().size()];
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(CommonUtil.dip2px(getActivity(), 10), CommonUtil.dip2px(getActivity(), 3));
            lp.setMargins(CommonUtil.dip2px(getActivity(), 3), 0, CommonUtil.dip2px(getActivity(), 3), 0);
            for (int i = 0; i < mAdBean.getAd_list().size(); i++) {
                ImageView indicatorImg = new ImageView(getActivity());
                if (i == 0) {
                    indicatorImg.setImageResource(R.drawable.lunbodian_sel);
                } else {
                    indicatorImg.setImageResource(R.drawable.lunbodian_nor);

                }
                indicatorImg.setLayoutParams(lp);
                centerIndicaLay.addView(indicatorImg);
                centerIndicators[i] = indicatorImg;
            }

        }
    }
    private void setTopIndicator(int selectedPosition) {
        if (topIndicators == null)
            return;
        for (int i = 0; i < topIndicators.length; i++) {
            topIndicators[i]
                    .setImageResource(R.drawable.lunbodian_nor);
        }
        if (topIndicators.length > selectedPosition)
            topIndicators[selectedPosition]
                    .setImageResource(R.drawable.lunbodian_sel);
    }
    private void setCenterIndicator(int selectedPosition) {
        if (centerIndicators == null)
            return;
        for (int i = 0; i < centerIndicators.length; i++) {
            centerIndicators[i]
                    .setImageResource(R.drawable.lunbodian_nor);
        }
        if (centerIndicators.length > selectedPosition)
            centerIndicators[selectedPosition]
                    .setImageResource(R.drawable.lunbodian_sel);
    }
    @Override
    public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {

        if (y < 0) {   //设置标题的背景颜色
            titleName.setTextColor(Color.argb((int) 0, 255, 255, 255));
            titleBar.setBackgroundColor(Color.argb((int) 0, 144, 151, 166));
            statusBar.setBackgroundColor(Color.argb((int) 0, 144, 151, 166));
        } else if (y >= 0 && y <= titleBarRange) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / titleBarRange;
            float alpha = (255 * scale);
            titleName.setTextColor(Color.argb((int) alpha, 255, 255, 255));
            titleBar.setBackgroundColor(Color.argb((int) alpha, 71, 158, 248));
            statusBar.setBackgroundColor(Color.argb((int) alpha, 71, 158, 248));
        } else {    //滑动到banner下面设置普通颜色
            titleName.setTextColor(Color.argb((int) 255, 255, 255, 255));
            titleBar.setBackgroundColor(Color.argb((int) 255, 71, 158, 248));
            statusBar.setBackgroundColor(Color.argb((int) 255, 71, 158, 248));

        }
    }



    public class ImageViewHolder implements Holder<ADInfo> {
        private ImageView imageView;
        private int placeHolderType;

        public ImageViewHolder(int placeHolderType) {
            this.placeHolderType=placeHolderType;
        }
        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, ADInfo data) {
            GlideUtil.getInstance().load(getActivity(), data.getAd_photo_url(), imageView, true,placeHolderType);
        }
    }
}
